﻿var _helper = new Helper();
var editIndex = undefined;
jQuery(document).ready(function () {

    jQuery('#queueMenu').addClass('active');
    jQuery('#queueAllList').datagrid({
        url: "/service/api.ashx?metdhodName=GetQueueAll",
        loadFilter: pagerFilter,
        onClickCell: onClickCell
    });
});


function pagerFilter(data) {
    if (typeof data.length == 'number' && typeof data.splice == 'function') {	// is array
        data = {
            total: data.length,
            rows: data
        }
    }
    var dg = jQuery(this);
    var opts = dg.datagrid('options');
    var pager = dg.datagrid('getPager');
    pager.pagination({
        onSelectPage: function (pageNum, pageSize) {
            opts.pageNumber = pageNum;
            opts.pageSize = pageSize;
            pager.pagination('refresh', {
                pageNumber: pageNum,
                pageSize: pageSize
            });
            dg.datagrid('loadData', data);
        }
    });
    if (!data.originalRows) {
        data.originalRows = (data.rows);
    }
    var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
    var end = start + parseInt(opts.pageSize);
    data.rows = (data.originalRows.slice(start, end));
    return data;
}

jQuery.extend(jQuery.fn.datagrid.methods, {
    editCell: function (jq, param) {
        return jq.each(function () {
            var opts = jQuery(this).datagrid('options');
            var fields = jQuery(this).datagrid('getColumnFields', true).concat(jQuery(this).datagrid('getColumnFields'));
            for (var i = 0; i < fields.length; i++) {
                var col = jQuery(this).datagrid('getColumnOption', fields[i]);
                col.editor1 = col.editor;
                if (fields[i] != param.field) {
                    col.editor = null;
                }
            }
            jQuery(this).datagrid('beginEdit', param.index);
            for (var i = 0; i < fields.length; i++) {
                var col = jQuery(this).datagrid('getColumnOption', fields[i]);
                col.editor = col.editor1;
            }
        });
    }
});

function endEditing() {
    if (editIndex == undefined) { return true }
    if (jQuery('#queueAllList').datagrid('validateRow', editIndex)) {
        jQuery('#queueAllList').datagrid('endEdit', editIndex);
        var rowEdited = jQuery('#queueAllList').datagrid('getRows')[editIndex];
        _helper.UpdateQueueStatus(rowEdited.id, rowEdited.IsActive, function (result) {
            var resultMessage = result;
            if (result.responseText) {
                resultMessage = "Update คิวที่ " + rowEdited.id + " Success";
            }
            updateStatusBar(resultMessage)
        });
        
        editIndex = undefined;
        return true;
    } else {
        return false;
    }
}

function onClickCell(index, field) {
    if (endEditing()) {
        jQuery('#queueAllList').datagrid('selectRow', index)
                .datagrid('editCell', { index: index, field: field });
        editIndex = index;
    }
}